package com.podium.generator.utils;

import com.alibaba.fastjson2.JSONObject;
import com.podium.comment.utils.StringUtils;
import com.podium.generator.entity.GeneratorTable;
import org.apache.velocity.VelocityContext;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 邵文泽
 * @description 模板工具类
 * @date 2025/4/27 8:54
 */
public class VelocityUtil {

    /**
     * 初始化模板参数
     *
     * @param generatorTable 数据
     * @return VelocityContext 模板数据
     */
    public static VelocityContext initVelocityParams(GeneratorTable generatorTable) {
        VelocityContext velocityContext = new VelocityContext();

        // 设置 JavaFieldBig 字段
        generatorTable.getGeneratorColumn().forEach(column ->
                column.setJavaFieldBig(StringUtils.capitalize(column.getJavaField()))
        );

        // 将 GeneratorTable 转换为 JSON 并注入上下文
        JSONObject.from(generatorTable).forEach(velocityContext::put);

        // 拆分表名
        String[] parts = generatorTable.getTableName().split("_");

        // 构建三个字段
        StringBuilder functionName = new StringBuilder();
        StringBuilder styleName = new StringBuilder();
        StringBuilder permisName = new StringBuilder();

        for (int i = 1; i < parts.length; i++) {
            String part = parts[i];
            functionName.append(part).append("/");
            styleName.append(part).append("-");
            permisName.append(part).append("_");
        }

        // 去除末尾多余的符号
        if (!functionName.isEmpty()) {
            functionName.setLength(functionName.length() - 1);
        }
        if (!styleName.isEmpty()) {
            styleName.setLength(styleName.length() - 1);
        }
        if (!permisName.isEmpty()) {
            permisName.setLength(permisName.length() - 1);
        }

        // 注入模板变量
        velocityContext.put("functionName", functionName.toString());
        velocityContext.put("styleName", styleName.toString());
        velocityContext.put("permitsName", permisName.toString());
        velocityContext.put("ClassName", generatorTable.getClassName());
        velocityContext.put("className", StringUtils.uncapitalize(generatorTable.getClassName()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        velocityContext.put("datetime",sdf.format(new Date()));

        return velocityContext;

    }

    /**
     * 获取模板列表
     *
     * @return List<String> 模板列表
     */
    public static List<String> getVelocityList() {
        List<String> list = new ArrayList<>();
        // 后端
        list.add("vm/package/Entity.java.vm");
        list.add("vm/package/Mapper.java.vm");
        list.add("vm/package/Service.java.vm");
        list.add("vm/package/ServiceImpl.java.vm");
        list.add("vm/package/Controller.java.vm");
        list.add("vm/package/Mapper.xml.vm");
        // 前端
        list.add("vm/ui/index.vue.vm");
        list.add("vm/ui/UnifiedForm.vue.vm");
        list.add("vm/ui/api.ts.vm");
        // SQL
        list.add("vm/sql/menuSql.sql.vm");
        return list;
    }

    /**
     * 根据模板名称获取文件名
     *
     * @param template 模板名称
     * @return 文件名
     */
    public static String getName(String template, GeneratorTable generatorTable) {
        return switch (template) {
            case "vm/package/Entity.java.vm" -> generatorTable.getClassName() + ".java";
            case "vm/package/Mapper.java.vm" -> generatorTable.getClassName() + "Mapper.java";
            case "vm/package/Service.java.vm" -> generatorTable.getClassName() + "Service.java";
            case "vm/package/ServiceImpl.java.vm" -> generatorTable.getClassName() + "ServiceImpl.java";
            case "vm/package/Controller.java.vm" -> generatorTable.getClassName() + "Controller.java";
            case "vm/package/Mapper.xml.vm" -> generatorTable.getClassName() + "Mapper.xml";
            case "vm/ui/index.vue.vm" -> "index.vue";
            case "vm/ui/UnifiedForm.vue.vm" -> "UnifiedForm.vue";
            case "vm/ui/api.ts.vm" -> generatorTable.getClassName() + ".ts";
            case "vm/sql/menuSql.sql.vm" -> "menuSql.sql";
            default -> throw new IllegalStateException("Unexpected value: " + template);
        };
    }

    /**
     * 根据模板名称获取文件路径
     *
     * @param template 模板名称
     * @return 文件路径
     */
    public static String getFileName(String template, GeneratorTable generatorTable) {
        return switch (template) {
            case "vm/package/Entity.java.vm" ->
                    "package/" + generatorTable.getPackageName() + "/entity/" + generatorTable.getClassName() + ".java";
            case "vm/package/Mapper.java.vm" ->
                    "package/" + generatorTable.getPackageName() + "/mapper/" + generatorTable.getClassName() + "Mapper.java";
            case "vm/package/Service.java.vm" ->
                    "package/" + generatorTable.getPackageName() + "/service/" + generatorTable.getClassName() + "Service.java";
            case "vm/package/ServiceImpl.java.vm" ->
                    "package/" + generatorTable.getPackageName() + "/service/impl/" + generatorTable.getClassName() + "ServiceImpl.java";
            case "vm/package/Controller.java.vm" ->
                    "package/" + generatorTable.getPackageName() + "/controller/" + generatorTable.getClassName() + "Controller.java";
            case "vm/package/Mapper.xml.vm" ->
                    "package/resources/mapper/web/" + generatorTable.getPackageName() + "/" + generatorTable.getClassName() + "Mapper.xml";
            case "vm/ui/index.vue.vm" ->
                    generatorTable.getPackageName() + "/" + generatorTable.getClassName() + "/index.vue";
            case "vm/ui/UnifiedForm.vue.vm" ->
                    generatorTable.getPackageName() + "/" + generatorTable.getClassName() + "/components/UnifiedForm.vue";
            case "vm/ui/api.ts.vm" ->
                    "api/" + generatorTable.getPackageName() + "/" + generatorTable.getClassName() + ".ts";
            case "vm/sql/menuSql.sql.vm" -> generatorTable.getClassName() + ".sql";
            default -> throw new IllegalStateException("Unexpected value: " + template);
        };
    }
}
